/**
 * Created by vincent on 2017/2/25.
 */
moduleCommon.service('UploadFileService', function ($q, $log, $$CambioConfig, ConstantService, Upload, LanguageService) {
    this.uploadFileForConfig = function (file, url, config) {
        var configBean = new UploadFileConfigBean(config);
        var d = $q.defer();
        if (StringUtils.isNlOrUndOrEmpty(url)) {
            url = $$CambioConfig.UPLOAD_URL;
        }
        url = url.format({zip: !!configBean.isZip});
        Upload.upload({
            url: url,
            data: {file: file}
        }).then(function (resp) {
            Alert.success(LanguageService.language('文件上传成功!'));
            $log.debug(LanguageService.language('文件上传结果!'), 'Success uploaded. Response: ' + JSON.stringify(resp.data));
            d.resolve(resp.data);
        }, function (resp) {
            Alert.error('File Upload Error！ status: ' + resp.status);
            d.reject(resp);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            if (configBean.progress) {
                configBean.progress.value = progressPercentage;
            }
        });
        return d.promise;
    }

    this.uploadFile = function (file, url, type, scope, isZip, showProgress, progress) {
        var d = $q.defer();
        if (StringUtils.isNlOrUndOrEmpty(url)) {
            url = $$CambioConfig.UPLOAD_URL;
        }
        url = url.format({zip: !!isZip});
        Upload.upload({
            url: url,
            data: {file: file}
        }).then(function (resp) {
            Alert.success(LanguageService.language('文件上传成功!'));
            $log.debug(LanguageService.language('文件上传结果!'), 'Success uploaded. Response: ' + JSON.stringify(resp.data));
            d.resolve(resp.data);
        }, function (resp) {
            Alert.error('File Upload Error！ status: ' + resp.status);
            d.reject(resp);
        }, function (evt) {
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
            if (type == 'IMG') {
                if (showProgress && progress) {
                    if (!showProgress) {
                        showProgress = true;
                        progress = {};
                    }
                    progress.value = progressPercentage;
                } else {
                    if (!scope.showPictureProgress) {
                        scope.showPictureProgress = true;
                        scope.pictureProgress = {};
                    }
                    scope.pictureProgress.value = progressPercentage;
                }

            }
            if (type == 'FILE') {
                if (showProgress && progress) {
                    if (!showProgress) {
                        showProgress = true;
                        progress = {};
                    }
                    progress.value = progressPercentage;
                } else {
                    if (!scope.showProgress) {
                        scope.showProgress = true;
                        scope.progress = {};
                    }
                    scope.progress.value = progressPercentage;
                }
            }
        });
        return d.promise;
    }
});